package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.BlackBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class BlackPresenter extends basePresenter {

    private Contract.BlackView blackView;

    public BlackPresenter(Contract.BlackView view) {
        super(view);
        this.blackView = view;
    }

    public void initData(int page, final boolean isRefersh, boolean isShow) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.blackList);
        rp.addBodyParameter("page", page + "");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, BlackBean.class, isShow, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                BlackBean bean = (BlackBean) baseBean;
                if (isRefersh) {
                    blackView.onRefresh(bean);
                } else {
                    blackView.onLoadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                blackView.onFail();
            }
        });
    }

    public void blackStateSave(final String type, String userid, final int position) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.blackStateSave);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("type", type);
        rp.addBodyParameter("userid", userid);
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                blackView.blackStateSaveSuccess(type, position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void removeBlackList(String userid, final int position) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.removeBlackList);
        rp.addBodyParameter("userid", userid);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                blackView.removeBlackListSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
